// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { readStorage } from "./CommonUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property({type: cc.AudioClip})
    btnAudioClip: cc.AudioClip = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        this.addBtnAudioEffect(this.btnAudioClip);
    }

    // update (dt) {}
    // 添加全局按钮点击音效
    addBtnAudioEffect(btnAudioClip) {
        // 全局按钮响应
        // 添加点击音效
        (function () {

            // let btnAudioClip = null;

            // cc.resources.load('audio/touch', cc.AudioClip, (err, clip) => {
            //     if (err) {
            //         cc.error(err.message || err);
            //         return;
            //     }

            //     btnAudioClip = clip;
            // });

            let Super = function () { };
            Super.prototype = cc.Button.prototype;
            //实例化原型
            Super.prototype._onTouchEnded = function (t) {
                if (this.interactable && this.enabledInHierarchy) {

                    if (btnAudioClip && btnAudioClip instanceof cc.AudioClip) {
                        let openSound = Number(readStorage('open_sound'));
                        if (openSound != 2) {
                            let audioID = cc.audioEngine.play(btnAudioClip, false, 0.5);
                        }
                    }

                    if (this._pressed) {
                        cc.Component.EventHandler.emitEvents(this.clickEvents, t);
                        this.node.emit('click', this);
                    }
                    this._pressed = !1;
                    this._updateState();
                    t.stopPropagation();
                }
            };
        })();
    }
}
